package sjtu.ist.warehouse4ws.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import sjtu.ist.warehouse4ws.epc2bpmn.GenerateBpmn;
import sjtu.ist.warehouse4ws.epc2bpmn.ParserEpc;
import sjtu.ist.warehouse4ws.tool.MyUtils;




import com.opensymphony.xwork2.ActionSupport;



public class GenerateBpmnAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3335592226930869947L;
	private GenerateBpmn generateBpmn;
	private String nodeName;


	public String getNodeName() {
		return nodeName;
	}
	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	public void setGenerateBpmn(GenerateBpmn generateBpmn) {
		this.generateBpmn = generateBpmn;
	}
	public String generateBpmn(){

		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/bpmn"));
		String basepath=ServletActionContext.getServletContext().getRealPath("/upload/files/epc/").replaceAll("\\\\", "/")+"/";
		//basepath is dynamic
		String outpath=ServletActionContext.getServletContext().getRealPath("/upload/files/bpmn/").replaceAll("\\\\", "/")+"/";


		HttpServletRequest request = ServletActionContext.getRequest ();
		String xmlName=request.getParameter("nodeName");
		String path=basepath+xmlName;
		
		String outFile=outpath+"BPMN_"+xmlName;
		
		try {
			System.out.println(path);
			generateBpmn.setDynamicPath(path);
			generateBpmn.writeBpmn(outFile, ParserEpc.parse(path));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
